package com.dst.smart.handler.disposer;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.music.MusicPlayMedia;
import com.dst.smart.player.entity.SongPlayInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
/**
 *唐诗宋词
 * 	给我背白居易的诗
 	我要听唐诗
 	静夜思是哪个朝代的诗
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class PoeTryDisposer extends Disposer{

	public PoeTryDisposer(Context context) {
		super(context);
	}

	@Override
	public void disposeResult(SemanticResult semanticResult) {

		try {
			if(MainActivity.musicIsSong) {
				if (semanticResult != null) {
					JSONArray result = semanticResult.getResult();
					if (result == null || result.length() == 0) return;
					JSONObject entry = result.getJSONObject(0);
					if (!TextUtils.isEmpty(entry.optString("content"))) {
						String conent = entry.getString("content");
						String title = entry.optString("title");
						getPlayController().playURLList(semanticResult.getUUID(), null);
						getPlayController().playText(semanticResult.getUUID(), title + "。" + conent, true, "", null);
					} else {
						List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
						for (int i = 0; i < result.length(); i++) {
							entry = result.getJSONObject(i);
							SongPlayInfo songPlayInfo = new SongPlayInfo();
							songPlayInfo.setPlayUrl("");
							songPlayInfo.setAnswerText(entry.optString("title") + entry.optString("content"));
							playList.add(songPlayInfo);
						}
						try {
							if (MusicPlayMedia.getInstansMedia() == null) {
								new MusicPlayMedia(mContext, "");
							}
							MusicPlayMedia.getInstansMedia().playURLList(playList);
							MusicPlayMedia.getInstansMedia().setFileName(1,
									playList.get(0).getPlayUrl(), 1);
							MusicPlayMedia.getInstansMedia().play();
					/*
					//先播放標題
					getPlayController().playURLList(semanticResult.getUUID(),null);
					getPlayController().playText(semanticResult.getUUID(),semanticResult.getAnswerText() , true, "", null);
					*/
						} catch (Exception e) {
							// TODO: handle exception
						}
						sendAiuiData("唐诗宋词", "poetry", result.toString());
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.POETRY);
	}

}
